var cusId;
var cusAccount;
var shopLongitude=0;//店铺经度
var shopLatitude=0;//店铺纬度
var EARTH_RADIUS = 6378.137;// 地球半径;单位米;6378.137千米
var imgUrl="http://121.42.189.191:80/";//全局变量-服务器地址


$(document).ready(function() {
	
	getLatitude();//获取map坐标
});

//点击店铺
function shopJump(shop){
	var shopUnique=$(shop).find("span.shopUnique").text();
	addCookie("shopUnique",shopUnique,0);
	window.location.href="index.html";
}


//手动查询商铺信息
function searchShop() {
	$("#imgLoading").show();//隐藏加载图片
	var shopName = $("#keyword").val().trim().toUpperCase();
	//http://121.42.189.191:8080/harricane
	$("#shop-shops").empty();
	$
			.ajax({
				url : "../online/findShopBySearch.do",
				type : "post",
				data : {
					"shopName":shopName,"shopLongitude":shopLongitude,"shopLatitude":shopLatitude},
				dataType : "json",
				success : function(result) {
					$("#imgLoading").css("display","none");//隐藏加载图片
					
					if (result.status == 1) {// 查询成功
					
						var data = result.data;
						var goods = "";
						for (var a = 0; a < data.length; a++) {
							var good = "";
							good += '<div class="list clearfloat fl" onclick="shopJump(this)">'
								+'<span style="display:none" class="shopUnique">'+data[a].shopUnique+'</span>'
							+ '<a>'
							+ '<div class="tu clearfloat ">'
							+ '<img src="'+imgUrl+data[a].shopImagePath+'"/>' + '</div>'
							+ '<div class="bottom clearfloat box-s ">'
							+ '<p class="over ">'+data[a].shopName+'</p>'
							+ '<span>'+data[a].shopAddressDetail+'</span>'
							+ '<p class="over ">'+GetDistance(shopLongitude,shopLatitude,data[a].shopLongitude,data[a].shopLatitude)+'km</p>' + '</div>' + '</a>'
							+ '</div>';
							
							goods += good;
						}
						$("#shop-shops").append(goods);
					} else {// 查询不到的话做出相应的提示信息
						
						var good = "";
						good += '<div class="list clearfloat fl" style="width:100%;height:100%">'
						+ '<a>'
						+ '<div class="tu clearfloat ">'
						+ '<img src="upload/no_goods.jpg"/>' + '</div>'
						+ '<div class="bottom clearfloat box-s ">'
						+ '<p class="over ">附近暂时还没有入驻的超市</p>'
						+ '<span></span>'
						+ '<p class="over "></p>' + '</div>' + '</a>'
						+ '</div>';
						
						$("#shop-shops").append(good);

					}
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {

				}
			});

}

// 获取当前坐标
function getLatitude() {
	// 默认地理位置设置为上海市浦东新区
	var x = 121.48789949, y = 31.24916171;

	if (navigator.geolocation) {
		navigator.geolocation.getCurrentPosition(showPosition);
		document.getElementById("status").innerHTML = "HTML5 Geolocation is supported in your browser.";
		// 百度地图API功能
		var map = new BMap.Map("container");
		var point = new BMap.Point(x, y);
		map.centerAndZoom(point, 12);

		var geolocation = new BMap.Geolocation();
		geolocation.getCurrentPosition(function(r) {
			if (this.getStatus() == BMAP_STATUS_SUCCESS) {
				var mk = new BMap.Marker(r.point);
				map.addOverlay(mk);
				map.panTo(r.point);
				console.log(r.point);
				console.log(r.point.lat+";"+r.point.lng);
				shopLongitude=r.point.lng;
			    shopLatitude=r.point.lat;
			    
			    searchShop();// 加载店铺信息
			    
				
			} else {
				alert('failed' + this.getStatus());
			}
		}, {
			enableHighAccuracy : true
		})
		return;
	}
	alert("你的浏览器不支持获取地理位置！");
}

// 显示位置
function showPosition(position) {
	x = position.coords.latitude;
	y = position.coords.longitude;
}



//
function  rad(d) {
	return d * Math.PI / 180.0;
}

//计算两个坐标之间的距离
function GetDistance(lat1, lng1, lat2, lng2) {
	
	var radLat1 = rad(lat1);
	var radLat2 = rad(lat2);
	var a = radLat1 - radLat2;
	var b = rad(lng1) - rad(lng2);

	var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
	s = s * EARTH_RADIUS;
	s = Math.round(s * 10000) / 10000;
	return s.toFixed(2);
}

